<?php
namespace Weibo\Controller;
use Common\Controller\AdminbaseController;

/**
 * 内容管理
 * @author Hongwei 2017-2-20
 */
class AdminWeiboController extends AdminbaseController {

	protected $weibo_model;
	protected $weibo_attachment_model;
	
	public function _initialize() {
		parent::_initialize();
		$this ->weibo_model = D('weibo');
		$this ->weibo_attachment_model = D('weibo_attachment');
	}

	/**
	 * 微博列表
	 */
	public function index() {

		$db_prefix=C('DB_PREFIX');

		$nickname = I('nickname');
		$create_time = I('create_time');

		if($nickname) {
			$where['nickname'] = array('like', "%$nickname%");
		}
		if($create_time ){
			$where['1 AND'] = array('exp', 'FROM_UNIXTIME(create_time, "%Y-%m-%d") = "'.$create_time.'"');
		}

		//页码
		$count = $weibo_list = $this->weibo_model
			-> alias('a')
			-> field('a.*,b.nickname')
			-> join($db_prefix.'weibo_user b ON a.user_id = b.user_id')
			-> where($where)
			-> count();
		$page = $this -> page($count, 10);

		//微博列表（所有的倒序排列）
		$weibo_list = $this->weibo_model
			-> alias('a')
			-> field('a.*,b.nickname')
			-> join($db_prefix.'weibo_user b ON a.user_id = b.user_id')
			-> where($where)
			-> limit($page->firstRow.','.$page->listRows)
			-> order(array('a.weibo_id'=>'desc'))
			-> select();
		//dump($weibo_list);
		$this -> assign('page', $page -> show('Admin'));
		$this -> assign('weibo_list', $weibo_list);
		$this -> display ();

		
	}
	
	/**
	 * 删除微博
	 */
	public function delete() {

		//删除操作
		$weibo_id= I('weibo_id');
		$result = $this->weibo_model -> delete($weibo_id);

		$data = array(
			'status' => $result ? true : false,
			'info' => $result ? '' : '删除失败，ID：'.$weibo_id,
			'url' => '',
		);
		$this -> ajaxReturn($data);

		//删除成功后通知作者	TODO 删除的逻辑操作是什么？用不用写到一张表上，然后通知作者？ 暂时关闭

		//$uid = $this->weibo_model->where($weibo_id)->find();
		//$message = "你好，你的这条信息违规则 ，被我们删除了！";
		//push_system_msg($uid, $message);

	}
	
	/**
	 * 附件列表
	 */
	public function attachments() {

		$db_prefix=C('DB_PREFIX');

		$nickname = I('nickname');
		$create_time = I('create_time');

		if($nickname) {
			$where['nickname'] = array('like', "%$nickname%");
		}
		if($create_time ){
			$where['1 AND'] = array('exp', 'FROM_UNIXTIME(create_time, "%Y-%m-%d") = "'.$create_time.'"');
		}

		//页码
		$count = $weibo_list = $this->weibo_attachment_model
			-> alias('a')
			-> field("a.*,b.nickname")
			-> join($db_prefix.'weibo_user b ON a.user_id = b.user_id')
			-> where($where)
			-> count();
		$page = $this -> page($count, 10);


		//列表
		$weibo_attachment_list = $this->weibo_attachment_model
			-> alias('a')
			-> field("a.*,b.nickname")
			-> join($db_prefix.'weibo_user b ON a.user_id = b.user_id')
			-> where($where)
			-> limit($page->firstRow.','.$page->listRows)
			-> order(array('attachment_id'=>'desc'))
			-> select();

		//dump($weibo_attachment_list);
		$this -> assign('page', $page -> show('Admin'));
		$this -> assign('list', $weibo_attachment_list);
		$this -> display ();

	}
	
	/**
	 * 删除附件
	 */
	public function deleteAttachment() {

		//删除操作
		$attachment_id= I('attachment_id');
		$result = $this->weibo_attachment_model -> delete($attachment_id);

		$data = array(
			'status' => $result ? true : false,
			'info' => $result ? '' : '删除失败，ID：'.$attachment_id,
			'url' => '',
		);
		$this -> ajaxReturn($data);

		//删除成功后通知作者	TODO 删除的逻辑操作是什么？用不用写到一张表上，然后通知作者？ 暂时关闭
		//$uid = $this->weibo_attachment_model->where($weibo_id)->find();
		//$message = "你好，你的这条信息违规则 ，被我们删除了！";
		//push_system_msg($uid, $message);

	}
	
}